'use strict';

stareal.config(function ($httpProvider, localStorageServiceProvider) {
    // 跨域
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
    // $httpProvider.defaults.headers.common['source']='mobile';  //增加访问识别标识
    // $httpProvider.defaults.headers.post ={'source':'mobile'};  //增加访问识别标识
    // 新增表头
    //$httpProvider.defaults.headers.common['Api-Invoker'] = stareal_wx
    localStorageServiceProvider
        .setPrefix('StarealWebApp')
        .setStorageType('sessionStorage');
}).factory('$api', function ($q, $http, localStorageService,base64) {
  //var host_prefix = "https://api.mydeershow.com/mobile/";
  //var host_prefix = "http://10.1.100.149/mobile/"; //本地
   //var host_prefix = "http://10.1.100.201/mobile/";
    //http://rvvr5v.natappfree.cc
   var host_prefix = "https://test.djx.api02.opentickets.cn/mobile/";
  // var host_prefix="http://111.231.0.33:9084/mobile/"; //测试 http://111.231.0.33:9084/mobile/app/main/category/list?source=pc api
 // var host_prefix="http://test.djx.api01.opentickets.cn/mobile/"; //测试
   var shopHost_prefix='https://test.djx.api02.opentickets.cn/mobile/'; //积分商城
 //var shopHost_prefix='https://api.mydeershow.com/mobile/'; //积分商城
    var service = {
        get: function (url, params, needToken){
            return handleRepData('get', url, params, needToken);
        },
        post: function (url, params, needToken){
            return handleRepData('post', url, params, needToken);
        }
    }
    function handleRepData(method, url, data, needToken){
        var params = data || {};
        if(params.source==undefined||params.source==null||params.source==''){
            params.source = 'mobile';//标识
        }
        if (needToken){
            params.accessToken = localStorageService.get('token');
        }
        if(params.citySituationId==undefined||params.citySituationId==null){
            params.citySituationId=localStorageService.get('cityid');
            params.citySituationName=localStorageService.get('cityname');
        }
        if(params.cityName==undefined|| params.cityName==null|| params.cityName==''){
            params.cityName=localStorageService.get('cityname');
            params.citySituationName=localStorageService.get('cityname');

        }
        var strdate=new Date().getTime().toString();
        var str1='',str2='';
        var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
            'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
            'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        for(var i=0; i<4; i++){
            var pos = Math.round(Math.random() * (arr.length-1));
            str1 += arr[pos];
        }
        for(var i=0; i<5; i++){
            var pos = Math.round(Math.random() * (arr.length-1));
            str2 += arr[pos];
        }
        var Str=str1+strdate+ str2;
        if(params.encrypt==undefined||params.encrypt==null||params.encrypt==''){
            params.encrypt=base64.encode(Str.toString());
        }
        if(params.shop==1){
            url = shopHost_prefix + url;
        }else{
            url = host_prefix + url;
        }
        var promise;
        var defer = $q.defer();
        switch (method) {
            case 'get':
                promise = $http({method: 'GET', url: url, params: params});
                break;
            case 'post':
                promise = $http({method: 'POST', url: url, params: params});
        }
        promise.then(function (rep) {
            if (rep.data.retCode == 0) {
                defer.resolve(rep.data);
            } else {
                defer.reject(rep.data.message);
            }
        }, function (err) {
            defer.reject(err);
        });
        return defer.promise;
    }

    return service;
}).factory('$lazyLoader', function ($api, $alert, $rootScope) {

    var LazyLoader = function (url, params, needToken, callback) {
        this.url = url;
        this.params = params || {};
        this.needToken = needToken;

        this.items = [];
        this.busy = false;
        this.end = false;
        this.nodata = false;

        this.params.pageNum = 0;
        this.params.pageSize = 10;

        // 兼容以下情況,草。
        this.params.page_num = 0;
        this.params.page_size = 10;

        this.callback = callback;
    };

    LazyLoader.prototype.nextPage = function () {
        if (this.busy) return;
        this.busy = true;
        this.params.pageNum += 1;
        this.params.page_num += 1;
        $api.get(this.url, this.params, this.needToken)
            .then(function (ret) {
                // console.log(ret);
                // console.log('2221');
                var items = ret.data;
                for (var i = 0; i < items.length; i++) {
                    this.items.push(items[i]);
                }
                if (ret.total_page == 0 ||ret.total_row==0) {
                    this.nodata = true;
                    return;
                }
                if (this.callback) {
                    var that = this;
                    this.callback.apply(null, [that]);
                }
                if (this.params.pageNum == ret.total_page) {
                    this.end = true;
                    return;
                }
                this.busy = false;
            }.bind(this));
    };
    return LazyLoader;
})